from common import data_check


def unknown_account(args):
    parameter_list = list(args.strip().split())
    if not len(parameter_list):
        return ""
    if len(parameter_list) % 2:
        return "只能输入成对数据！"
    data_check_result = data_check(parameter_list, 1, 2, 'ip')
    if not data_check_result[0]:
        return f"数据格式错误，{data_check_result[1]}不是IP地址"

    account_host_dict = {}
    for index, parameter in enumerate(parameter_list):
        if not index % 2:
            if account_host_dict.get(parameter_list[index + 1]) is None:
                account_host_dict[parameter_list[index + 1]] = []
            account_host_dict[parameter_list[index + 1]].append(parameter)
    result = ''
    print("account_host_dict_len = ", len(account_host_dict))
    if len(account_host_dict) != 1: result += 'begin\n'
    for index, item in enumerate(account_host_dict.items()):
        key, value = item
        value = str(value).replace('[', '').replace(']', '').replace(' ', '')
        result += f"update uap_host_acct set acct_type ='01' where host_id in (select host_id from uap_host_ip where ip in ({value})) and acct_name = '{key}';\n"
    if len(account_host_dict) != 1: result += 'end;'
    return result


def host_status(args):
    parameter_list = list(args.strip().split())
    print(parameter_list)
    print(len(parameter_list))
    if not len(parameter_list):
        return ""

    data_check_result = data_check(parameter_list, 1, 1, 'ip')
    if not data_check_result[0]:
        return f"数据格式错误，{data_check_result[1]}不是IP地址"

    parameter_str = str(parameter_list).replace('[', '').replace(']', '').replace(' ', '')

    return f"update uap_host set SYNC_STATUS ='0' where host_id in(select host_id from uap_host_ip where ip in({parameter_str}));"


def host_version(args):
    parameter_list = list(args.strip().split())
    print(parameter_list)
    print(len(parameter_list))
    if not len(parameter_list):
        return ""

    data_check_result = data_check(parameter_list, 1, 1, 'ip')
    if not data_check_result[0]:
        return f"数据格式错误，{data_check_result[1]}不是IP地址"

    parameter_str = str(parameter_list).replace('[', '').replace(']', '').replace(' ', '')

    return f"Update uap_host a set a.res_manu_id='10028',a.res_series_id='160500',a.res_version_id='160503'   where host_id in (select host_id from uap_host_ip where ip in ({parameter_str}));"


def change_certificate(args):
    parameter_list = list(args.strip().split())

    if len(parameter_list) % 2:
        return "只能输入成对数据！"
    data_check_result = data_check(parameter_list, 1, 2, 'ip')
    if not data_check_result[0]:
        return f"数据格式错误，{data_check_result[1]}不是IP地址"

    account_host_dict = {}
    for index, parameter in enumerate(parameter_list):
        if not index % 2:
            if account_host_dict.get(parameter_list[index + 1]) is None:
                account_host_dict[parameter_list[index + 1]] = []
            account_host_dict[parameter_list[index + 1]].append(parameter)
    result = ''
    if len(account_host_dict) > 1: result += 'begin\n'
    for index, item in enumerate(account_host_dict.items()):
        key, value = item
        value = str(value).replace('[', '').replace(']', '').replace(' ', '')
        result += f"update uap_host_acct set authen_mode ='1' where host_id in (select host_id from uap_host_ip where ip in ({value})) and acct_name = '{key}';\n"
    if len(account_host_dict) > 1: result += 'end;'
    return result
